var yz_pay = require("../../../mycomponent/yz_pay/yz_pay");
import {payKeyboardAsync} from "../../../mycomponent/payKeyboard/PayKeyboardAsync.js";
const app = getApp();
Component({
  behaviors: [yz_pay],
  options: {
    styleIsolation: 'shared' 
  },
  properties: {
    overlayShow:{
      type: Boolean,
      value: false,
    },
    newBlindBoxData: {
      type: null,
      value: {},
    }
  },
  lifetimes: {
    attached() {
      // 在组件实例刚刚被创建时执行
      this.getOpenData();
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    postageShow: false,
    integralShow: false,
    actionSheetShow: false,
    btnData: [], //支付方式集合,
    allData: {},
    postageNum: "",
    isDeliverGoods: true , //只点击一次发货，多次后不可以点击,
    payKeyboardShow: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 获取数据
    getOpenData(){
      app._postNetWork({
        url: app.getNetAddresss(this.data.newBlindBoxData.api),
        data: { order_id: this.data.newBlindBoxData.order_id },
        success: ({data}) => {
          if(data.result) {
            this.setData({
              allData: data.data,
            })
          }else {
            wx.showToast({
              icon: 'none',
              title: data.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      })
    },
    close() {
      this.triggerEvent("issueGoodsClose", false )
    },
    // 点击我要了
    clickWant() {
      if(!this.data.allData.canBuy) {
        return
      }
      if (this.data.allData.need_freight) {
        this.getpPstageData();
      } else {
        if (this.data.isDeliverGoods) {
          this.deliverGoods();
        }
      }
    },
     // 不需要运费就直接发货
    deliverGoods() {
      app._postNetWork({
        url: app.getNetAddresss("plugin.new-blind-box.frontend.activity.buy-blind-box"),
        data: { order_id: this.data.newBlindBoxData.order_id },
        success: ({data}) => {
          if(data.result) {
            wx.showToast({
              icon: 'none',
              title: data.msg,
              duration: 1500
            });
            this.setData({
              overlayShow: false,
              isDeliverGoods: false
            });
             // 获取最新数据
            wx.navigateTo({
              url: "/packageA/member/myOrder_v2/myOrder_v2?status=0&orderType=newBindBox&title="+'我的盲盒',
            });
          }else {
            wx.showToast({
              icon: 'none',
              title: data.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      })
     },
    // 需要运费
    getpPstageData() {
      app._postNetWork({
        url: app.getNetAddresss("plugin.new-blind-box.frontend.goods-buy.index"),
        data: { activity_order_id: this.data.newBlindBoxData.order_id },
        success: ({data}) => {
          if(data.result) {
            for(let item of data.data.amount_items) {
              if(item.code == 'total_goods_price') {
                this.setData({
                  postageNum: item.amount
                })
              }
            }
            this.setData({
              postageShow: true
            });
          }else {
            wx.showToast({
              icon: 'none',
              title: data.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      })
    },
    clickClosePostage() {
      this.setData({
        postageShow: false
      })
    },
    // 兑换
    exchange() {
      this.setData({
        integralShow: true
      })
    },
    cancal() {
      this.setData({
        integralShow: false
      })
    },
    //获取支付类型参数
    clickPay() {
      app._postNetWork({
        url: app.getNetAddresss("plugin.new-blind-box.frontend.create.index"),
        data: { activity_order_id: this.data.newBlindBoxData.order_id },
        success: ({data}) => {
          if (data.result == 1) {
            this.getPayData(data.data.order_ids);
          } else {
            wx.showToast({
              title: data.msg,
              duration: 1000,
              icon: "none",
            });
          }
        },
      });
    },
    getPayData(order_ids) {
      app._postNetWork({
        url: app.getNetAddresss("order.merge-pay"),
        data: { order_ids },
        success: ({data}) => {
          if (data.result == 1) {
            this.setData({
              btnData: data.data.buttons,
              actionSheetShow: true,
              order_sn: data.data.order_pay.pay_sn
            });
            this.data.order_pay_id =  data.data.order_pay.id;
          } else {
            wx.showToast({
              title: data.msg,
              duration: 1000,
              icon: "none",
            });
          }
        },
      });
    },
    // 取消支付
    rewardPayCancelBtn() {
      this.setData({
        actionSheetShow: false
      });
    },
    // 兑换积分
    exchangeIntegral() {
      app._postNetWork({
        url: app.getNetAddresss('plugin.new-blind-box.frontend.activity.exchange'),
        data: { order_id: this.data.newBlindBoxData.order_id },
        success: ({data}) => {
          if (data.result == 1) {
            wx.showToast({
              title: data.msg,
              duration: 1000,
              icon: "none",
            });
            this.setData({
              integralShow: false,
              overlayShow: false
            });
            wx.navigateTo({
              url: "/packageA/member/myOrder_v2/myOrder_v2?status=0&orderType=newBindBox&title="+'我的盲盒',
            });
          } else {
            wx.showToast({
              title: data.msg,
              duration: 1000,
              icon: "none",
            });
          }
        },
      });
    },
    async payPost(value) {
      let type = value.currentTarget.dataset.info.value;
      let btn = value.currentTarget.dataset.info;
      if (btn.need_password) {
        let pass = await this.getPayKeyboardPassword();
        this.getVerifyPassword(pass);
      } else {
        this.handlePay(btn, type);
      }
    },
    // 校验密码
    getVerifyPassword(pwd){
      let urlStr = app.getNetAddresss("payment.password.check");
      urlStr += "&password=" + pwd;
      app._getNetWork({
        url: urlStr,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.balancePay(pwd);
          } else {
            wx.showModal({
              title: '提示',
              content: res.msg,
              success(restext) {
                if (restext.confirm) {
                  if (res.data.code == 2001) {
                    //商城支付密码设置未开启
                  } else if (res.data.code == 2002) {
                    //用户未设置支付密码
                    //去设置密码
                    wx.navigateTo({
                      url: '/packageA/member/set_balance_password/set_balance_password'
                    });
                  } else if (res.data.code == 2003) {
                    //支付密码错误
                  }
                } else if (restext.cancel) {
                  if (res.data.code == 2001) {
                    //商城支付密码设置未开启
                  } else if (res.data.code == 2002) {
                    //用户未设置支付密码
                  } else if (res.data.code == 2003) {
                    //支付密码错误
                  }
                }
              }
            });
          }
        },
        fail: (res) => {
          console.log(res);
        },
      });
    },
    payKeyboardComplete(evt){
      payKeyboardAsync.complete(evt.detail);
    },
    getPayKeyboardPassword(){
      this.setData({
        payKeyboardShow:true
      });
      return new Promise((resove,reject)=>{
        payKeyboardAsync.addCompleteFn((pass)=>{
          resove(pass);
        });
      });
    },
    payKeyboardClose(){
      this.setData({payKeyboardShow:false});
    },
  }
})
